

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>federatedml.ftl.plain_ftl &mdash; FATE 1.0 documentation</title>
  

  
  
  
  

  
  <script type="text/javascript" src="../../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../../" src="../../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    <link rel="index" title="Index" href="../../../genindex.html" />
    <link rel="search" title="Search" href="../../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../../index.html" class="icon icon-home"> FATE
          

          
          </a>

          
            
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <!-- Local TOC -->
              <div class="local-toc"></div>
            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">FATE</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>federatedml.ftl.plain_ftl</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for federatedml.ftl.plain_ftl</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1">#  Copyright 2019 The FATE Authors. All Rights Reserved.</span>
<span class="c1">#</span>
<span class="c1">#  Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);</span>
<span class="c1">#  you may not use this file except in compliance with the License.</span>
<span class="c1">#  You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1">#      http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1">#  Unless required by applicable law or agreed to in writing, software</span>
<span class="c1">#  distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1">#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1">#  See the License for the specific language governing permissions and</span>
<span class="c1">#  limitations under the License.</span>
<span class="c1">#</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">arch.api.utils</span> <span class="k">import</span> <span class="n">log_utils</span>

<span class="n">LOGGER</span> <span class="o">=</span> <span class="n">log_utils</span><span class="o">.</span><span class="n">getLogger</span><span class="p">()</span>

<span class="c1"># from federatedml.optim.activation import sigmoid</span>


<div class="viewcode-block" id="sigmoid"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.sigmoid">[docs]</a><span class="k">def</span> <span class="nf">sigmoid</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
    <span class="k">return</span> <span class="mf">1.</span> <span class="o">/</span> <span class="p">(</span><span class="mf">1.</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">exp</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">))</span></div>


<div class="viewcode-block" id="PartyModelInterface"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PartyModelInterface">[docs]</a><span class="k">class</span> <span class="nc">PartyModelInterface</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

<div class="viewcode-block" id="PartyModelInterface.send_components"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PartyModelInterface.send_components">[docs]</a>    <span class="k">def</span> <span class="nf">send_components</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="PartyModelInterface.receive_components"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PartyModelInterface.receive_components">[docs]</a>    <span class="k">def</span> <span class="nf">receive_components</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">components</span><span class="p">):</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="PartyModelInterface.send_gradients"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PartyModelInterface.send_gradients">[docs]</a>    <span class="k">def</span> <span class="nf">send_gradients</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="PartyModelInterface.receive_gradients"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PartyModelInterface.receive_gradients">[docs]</a>    <span class="k">def</span> <span class="nf">receive_gradients</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">gradients</span><span class="p">):</span>
        <span class="k">pass</span></div>

<div class="viewcode-block" id="PartyModelInterface.predict"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PartyModelInterface.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">):</span>
        <span class="k">pass</span></div></div>


<div class="viewcode-block" id="PlainFTLGuestModel"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel">[docs]</a><span class="k">class</span> <span class="nc">PlainFTLGuestModel</span><span class="p">(</span><span class="n">PartyModelInterface</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">local_model</span><span class="p">,</span> <span class="n">model_param</span><span class="p">,</span> <span class="n">is_trace</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">PlainFTLGuestModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span> <span class="o">=</span> <span class="n">local_model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feature_dim</span> <span class="o">=</span> <span class="n">local_model</span><span class="o">.</span><span class="n">get_encode_dim</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">model_param</span><span class="o">.</span><span class="n">alpha</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_trace</span> <span class="o">=</span> <span class="n">is_trace</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">LOGGER</span>

<div class="viewcode-block" id="PlainFTLGuestModel.set_batch"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.set_batch">[docs]</a>    <span class="k">def</span> <span class="nf">set_batch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">non_overlap_indexes</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">X</span> <span class="o">=</span> <span class="n">X</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y</span> <span class="o">=</span> <span class="n">y</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">non_overlap_indexes</span> <span class="o">=</span> <span class="n">non_overlap_indexes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">overlap_indexes</span> <span class="o">=</span> <span class="n">overlap_indexes</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">phi</span> <span class="o">=</span> <span class="kc">None</span></div>

    <span class="k">def</span> <span class="nf">__compute_phi</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">uA</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
        <span class="n">length_y</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">y</span> <span class="o">*</span> <span class="n">uA</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">/</span> <span class="n">length_y</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_compute_components</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uA</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">)</span>
        <span class="c1"># phi has shape (1, feature_dim)</span>
        <span class="c1"># phi_2 has shape (feature_dim, feature_dim)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__compute_phi</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uA</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">phi_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">phi</span><span class="o">.</span><span class="n">transpose</span><span class="p">(),</span> <span class="bp">self</span><span class="o">.</span><span class="n">phi</span><span class="p">)</span>

        <span class="c1"># y_overlap and y_overlap_2 have shape (len(overlap_indexes), 1)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">overlap_indexes</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_trace</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;phi shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">phi</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;phi_2 shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">phi_2</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;y_overlap shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;y_overlap_2 shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>

        <span class="c1"># following two parameters will be sent to host</span>
        <span class="c1"># y_overlap_2_phi_2 has shape (len(overlap_indexes), feature_dim, feature_dim)</span>
        <span class="c1"># y_overlap_phi has shape (len(overlap_indexes), feature_dim)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2_phi_2</span> <span class="o">=</span> <span class="mf">0.25</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">phi_2</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_phi</span> <span class="o">=</span> <span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">phi</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">uA_overlap</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uA</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">overlap_indexes</span><span class="p">]</span>
        <span class="c1"># mapping_comp_A has shape (len(overlap_indexes), feature_dim)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_A</span> <span class="o">=</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">uA_overlap</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_dim</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_trace</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;y_overlap_2_phi_2 shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2_phi_2</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;y_overlap_phi shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_phi</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;mapping_comp_A shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_A</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>

<div class="viewcode-block" id="PlainFTLGuestModel.send_components"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.send_components">[docs]</a>    <span class="k">def</span> <span class="nf">send_components</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_compute_components</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2_phi_2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_phi</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_A</span><span class="p">]</span></div>

<div class="viewcode-block" id="PlainFTLGuestModel.receive_components"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.receive_components">[docs]</a>    <span class="k">def</span> <span class="nf">receive_components</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">components</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span> <span class="o">=</span> <span class="n">components</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap_2</span> <span class="o">=</span> <span class="n">components</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_B</span> <span class="o">=</span> <span class="n">components</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_gradients</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_loss</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_update_gradients</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>

        <span class="c1"># y_overlap_2 have shape (len(overlap_indexes), 1),</span>
        <span class="c1"># phi has shape (1, feature_dim),</span>
        <span class="c1"># y_overlap_2_phi has shape (len(overlap_indexes), 1, feature_dim)</span>
        <span class="n">y_overlap_2_phi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">phi</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="c1"># uB_overlap_2 has shape (len(overlap_indexes), feature_dim, feature_dim)</span>
        <span class="c1"># loss_grads_const_part1 has shape (len(overlap_indexes), feature_dim)</span>
        <span class="n">loss_grads_const_part1</span> <span class="o">=</span> <span class="mf">0.25</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">y_overlap_2_phi</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap_2</span><span class="p">),</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="c1"># loss_grads_const_part2 has shape (len(overlap_indexes), feature_dim)</span>
        <span class="n">loss_grads_const_part2</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_trace</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;loss_grads_const_part1 shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">loss_grads_const_part1</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;loss_grads_const_part2 shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">loss_grads_const_part2</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;y_overlap shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;uB_overlap shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>

        <span class="n">const</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">loss_grads_const_part1</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span> <span class="o">-</span> <span class="mf">0.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">loss_grads_const_part2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
        <span class="c1"># grad_A_nonoverlap has shape (len(non_overlap_indexes), feature_dim)</span>
        <span class="c1"># grad_A_overlap has shape (len(overlap_indexes), feature_dim)</span>
        <span class="n">grad_A_nonoverlap</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">*</span> <span class="n">const</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">non_overlap_indexes</span><span class="p">]</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
        <span class="n">grad_A_overlap</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">*</span> <span class="n">const</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_B</span>

        <span class="n">loss_grad_A</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">),</span> <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="o">.</span><span class="n">shape</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
        <span class="n">loss_grad_A</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">non_overlap_indexes</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">grad_A_nonoverlap</span>
        <span class="n">loss_grad_A</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">overlap_indexes</span><span class="p">,</span> <span class="p">:]</span> <span class="o">=</span> <span class="n">grad_A_overlap</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss_grads</span> <span class="o">=</span> <span class="n">loss_grad_A</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">backpropogate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">,</span> <span class="n">loss_grad_A</span><span class="p">)</span>

<div class="viewcode-block" id="PlainFTLGuestModel.send_loss"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.send_loss">[docs]</a>    <span class="k">def</span> <span class="nf">send_loss</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss</span></div>

<div class="viewcode-block" id="PlainFTLGuestModel.receive_loss"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.receive_loss">[docs]</a>    <span class="k">def</span> <span class="nf">receive_loss</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">loss</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss</span> <span class="o">=</span> <span class="n">loss</span></div>

    <span class="k">def</span> <span class="nf">_update_loss</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">uA_overlap</span> <span class="o">=</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">uA_overlap</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_dim</span>
        <span class="n">loss_overlap</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">uA_overlap</span> <span class="o">*</span> <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="p">)</span>
        <span class="n">loss_y</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__compute_loss_y</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">phi</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">*</span> <span class="n">loss_y</span> <span class="o">+</span> <span class="n">loss_overlap</span>

    <span class="k">def</span> <span class="nf">__compute_loss_y</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">uB_overlap</span><span class="p">,</span> <span class="n">y_overlap</span><span class="p">,</span> <span class="n">phi</span><span class="p">):</span>
        <span class="c1"># uB_phi has shape (len(overlap_indexes), 1)</span>
        <span class="n">uB_phi</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">uB_overlap</span><span class="p">,</span> <span class="n">phi</span><span class="o">.</span><span class="n">transpose</span><span class="p">())</span>
        <span class="n">loss_y</span> <span class="o">=</span> <span class="p">(</span><span class="o">-</span><span class="mf">0.5</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">y_overlap</span> <span class="o">*</span> <span class="n">uB_phi</span><span class="p">)</span> <span class="o">+</span> <span class="mf">1.0</span> <span class="o">/</span> <span class="mi">8</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">uB_phi</span> <span class="o">*</span> <span class="n">uB_phi</span><span class="p">))</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">y_overlap</span><span class="p">)</span> <span class="o">*</span> <span class="n">np</span><span class="o">.</span><span class="n">log</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">loss_y</span>

<div class="viewcode-block" id="PlainFTLGuestModel.get_loss_grads"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.get_loss_grads">[docs]</a>    <span class="k">def</span> <span class="nf">get_loss_grads</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss_grads</span></div>

<div class="viewcode-block" id="PlainFTLGuestModel.predict"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">uB</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">phi</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">uA</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">phi</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__compute_phi</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uA</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">sigmoid</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">uB</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">phi</span><span class="o">.</span><span class="n">transpose</span><span class="p">()))</span></div>

<div class="viewcode-block" id="PlainFTLGuestModel.restore_model"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.restore_model">[docs]</a>    <span class="k">def</span> <span class="nf">restore_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_parameters</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">restore_model</span><span class="p">(</span><span class="n">model_parameters</span><span class="p">)</span></div>

<div class="viewcode-block" id="PlainFTLGuestModel.get_model_parameters"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLGuestModel.get_model_parameters">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">get_model_parameters</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="PlainFTLHostModel"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel">[docs]</a><span class="k">class</span> <span class="nc">PlainFTLHostModel</span><span class="p">(</span><span class="n">PartyModelInterface</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">local_model</span><span class="p">,</span> <span class="n">model_param</span><span class="p">,</span> <span class="n">is_trace</span><span class="o">=</span><span class="kc">False</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">PlainFTLHostModel</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span> <span class="o">=</span> <span class="n">local_model</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">feature_dim</span> <span class="o">=</span> <span class="n">local_model</span><span class="o">.</span><span class="n">get_encode_dim</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">=</span> <span class="n">model_param</span><span class="o">.</span><span class="n">alpha</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">is_trace</span> <span class="o">=</span> <span class="n">is_trace</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">logger</span> <span class="o">=</span> <span class="n">LOGGER</span>

<div class="viewcode-block" id="PlainFTLHostModel.set_batch"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel.set_batch">[docs]</a>    <span class="k">def</span> <span class="nf">set_batch</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">X</span> <span class="o">=</span> <span class="n">X</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">overlap_indexes</span> <span class="o">=</span> <span class="n">overlap_indexes</span></div>

    <span class="k">def</span> <span class="nf">_compute_components</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uB</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">)</span>

        <span class="c1"># following three parameters will be sent to guest</span>
        <span class="c1"># uB_overlap has shape (len(overlap_indexes), feature_dim)</span>
        <span class="c1"># uB_overlap_2 has shape (len(overlap_indexes), feature_dim, feature_dim)</span>
        <span class="c1"># mapping_comp_B has shape (len(overlap_indexes), feature_dim)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">uB</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">overlap_indexes</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">2</span><span class="p">),</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_B</span> <span class="o">=</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">feature_dim</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">is_trace</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;uB_overlap shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;uB_overlap_2 shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap_2</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;mapping_comp_B shape&quot;</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_B</span><span class="o">.</span><span class="n">shape</span><span class="p">))</span>

<div class="viewcode-block" id="PlainFTLHostModel.send_components"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel.send_components">[docs]</a>    <span class="k">def</span> <span class="nf">send_components</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_compute_components</span><span class="p">()</span>
        <span class="k">return</span> <span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap_2</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_B</span><span class="p">]</span></div>

<div class="viewcode-block" id="PlainFTLHostModel.receive_components"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel.receive_components">[docs]</a>    <span class="k">def</span> <span class="nf">receive_components</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">components</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2_phi_2</span> <span class="o">=</span> <span class="n">components</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_phi</span> <span class="o">=</span> <span class="n">components</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_A</span> <span class="o">=</span> <span class="n">components</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_gradients</span><span class="p">()</span></div>

    <span class="k">def</span> <span class="nf">_update_gradients</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c1"># uB_overlap_ex has shape (len(overlap_indexes), 1, feature_dim)</span>
        <span class="n">uB_overlap_ex</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">expand_dims</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">uB_overlap</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>

        <span class="c1"># y_overlap_2_phi_2 has shape (len(overlap_indexes), feature_dim, feature_dim)</span>
        <span class="c1"># uB_overlap_y_overlap_2_phi_2 has shape (len(overlap_indexes), 1, feature_dim)</span>
        <span class="n">uB_overlap_y_overlap_2_phi_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">matmul</span><span class="p">(</span><span class="n">uB_overlap_ex</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_2_phi_2</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">overlap_uB_y_overlap_2_phi_2</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">uB_overlap_y_overlap_2_phi_2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
        <span class="c1"># y_overlap_phi has shape (len(overlap_indexes), feature_dim)</span>
        <span class="n">l1_grad_B</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">squeeze</span><span class="p">(</span><span class="n">uB_overlap_y_overlap_2_phi_2</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">y_overlap_phi</span>
        <span class="n">loss_grad_B</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">alpha</span> <span class="o">*</span> <span class="n">l1_grad_B</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">mapping_comp_A</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">loss_grads</span> <span class="o">=</span> <span class="n">loss_grad_B</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">backpropogate</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">X</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">overlap_indexes</span><span class="p">],</span> <span class="kc">None</span><span class="p">,</span> <span class="n">loss_grad_B</span><span class="p">)</span>

<div class="viewcode-block" id="PlainFTLHostModel.get_loss_grads"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel.get_loss_grads">[docs]</a>    <span class="k">def</span> <span class="nf">get_loss_grads</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">loss_grads</span></div>

<div class="viewcode-block" id="PlainFTLHostModel.predict"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">transform</span><span class="p">(</span><span class="n">X</span><span class="p">)</span></div>

<div class="viewcode-block" id="PlainFTLHostModel.restore_model"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel.restore_model">[docs]</a>    <span class="k">def</span> <span class="nf">restore_model</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">model_parameters</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">restore_model</span><span class="p">(</span><span class="n">model_parameters</span><span class="p">)</span></div>

<div class="viewcode-block" id="PlainFTLHostModel.get_model_parameters"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.PlainFTLHostModel.get_model_parameters">[docs]</a>    <span class="k">def</span> <span class="nf">get_model_parameters</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">localModel</span><span class="o">.</span><span class="n">get_model_parameters</span><span class="p">()</span></div></div>


<div class="viewcode-block" id="LocalPlainFederatedTransferLearning"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.LocalPlainFederatedTransferLearning">[docs]</a><span class="k">class</span> <span class="nc">LocalPlainFederatedTransferLearning</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">guest</span><span class="p">:</span> <span class="n">PlainFTLGuestModel</span><span class="p">,</span> <span class="n">host</span><span class="p">:</span> <span class="n">PlainFTLHostModel</span><span class="p">,</span> <span class="n">private_key</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="nb">super</span><span class="p">(</span><span class="n">LocalPlainFederatedTransferLearning</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="fm">__init__</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">guest</span> <span class="o">=</span> <span class="n">guest</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host</span> <span class="o">=</span> <span class="n">host</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">private_key</span> <span class="o">=</span> <span class="n">private_key</span>

<div class="viewcode-block" id="LocalPlainFederatedTransferLearning.fit"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.LocalPlainFederatedTransferLearning.fit">[docs]</a>    <span class="k">def</span> <span class="nf">fit</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X_A</span><span class="p">,</span> <span class="n">X_B</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="p">,</span> <span class="n">non_overlap_indexes</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">guest</span><span class="o">.</span><span class="n">set_batch</span><span class="p">(</span><span class="n">X_A</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">non_overlap_indexes</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="o">.</span><span class="n">set_batch</span><span class="p">(</span><span class="n">X_B</span><span class="p">,</span> <span class="n">overlap_indexes</span><span class="p">)</span>
        <span class="n">comp_B</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="o">.</span><span class="n">send_components</span><span class="p">()</span>
        <span class="n">comp_A</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">guest</span><span class="o">.</span><span class="n">send_components</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">guest</span><span class="o">.</span><span class="n">receive_components</span><span class="p">(</span><span class="n">comp_B</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="o">.</span><span class="n">receive_components</span><span class="p">(</span><span class="n">comp_A</span><span class="p">)</span>
        <span class="n">loss</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">guest</span><span class="o">.</span><span class="n">send_loss</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">loss</span></div>

<div class="viewcode-block" id="LocalPlainFederatedTransferLearning.predict"><a class="viewcode-back" href="../../../federatedml.ftl.html#federatedml.ftl.plain_ftl.LocalPlainFederatedTransferLearning.predict">[docs]</a>    <span class="k">def</span> <span class="nf">predict</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">X_B</span><span class="p">):</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">host</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">X_B</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">guest</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span></div></div>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2019, FATE_TEAM

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>